<?php

namespace app\cps\controller;

use app\cps\service\LinkService;
use think\facade\Request;
use app\common\Common;
use app\common\Error;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Log;
use think\facade\Queue;

/**
 * 短剧推广
 */
class Link extends Base
{
    private $service;

    public function __construct(LinkService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染获取推广链接页
    public function info()
    {
        return view();
    }

    // 渲染推广链接列表页
    public function index()
    {
        return view();
    }

    // 添加推广链接
    public function add()
    {
        $params = Request::param();
        try {
            validate(\app\cps\validate\Link::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->editLink($params, $this->getUserInfo());

        if (!empty($data['message'])) {
            $message = !empty($data['message']) ? $data['message'] : Error::FAIL_MESSAGE;
			return Common::fail($message);
		}

		return Common::success($data);
    }

    // 编辑推广链接
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Link::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editLink($params, $this->getUserInfo());

        if ($result) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 获取投放媒体
    public function getLaunchMedia()
    {
        $data = config('enum.launch_media');
        return Common::success($data);
    }

    // 推广链接列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getLinkList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 推广链接详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Link::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getLinkDetail($params['id']);

        return Common::success($data);
    }

    // 收藏/取消收藏
    public function collection()
    {
        $params = Request::param();
        try {
            validate(\app\cps\validate\Link::class)->scene('collection')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->collectionLink($params['id'], $this->getUserInfo());

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 投放回调 抖音1.0监测链接回调地址
    public function monitoringLink()
    {
        $params = $_GET;
        if (empty($params['callback_param'])) {
            return;
        }

        $this->service->saveMonitoringLinkData($params);
    }

    // 抖音2.0监测链接回调地址
    public function tiktokMonitoringLink()
    {
        $params = $_GET;
        if (empty($params['callback_param'])) {
            return;
        }

        $this->service->saveTiktokMonitoringLinkData($params);
    }

    // 直接调队列测试回传
    public function test()
    {
        $back = new \app\job\PostBack();
        $info = Db::name('pay_record')->where(['id' => 3279450])->field('id, number, type, uid, link_id, total_fee, status, advertiser_id, activation_time')->find();

        $userInfo = Db::name('user')->where('id', $info['uid'])->field('id, money, expiration_time, first_charge_date, clickid, interpolate_id, activation_time, ip, ua, applet_id, open_id, back_url')->find();

        if ($info['link_id'] && $userInfo['clickid'] && $info['advertiser_id']) {
            $info['link_id'] = 2584;
            $info['clickid'] = $userInfo['clickid'];
            $info['open_id'] = $userInfo['open_id'];
            $info['back_url'] = $userInfo['back_url'];
            $info['applet_id'] = $userInfo['applet_id'];
            // dump($info);die;
            $result = $back->fire($info);
            dump($result);die;
        }
    }

    // 巨量开发者平台回调地址
    public function callback()
    {
        $params = Request::param();
        file_put_contents('./juliang_callback.txt', date('Y-m-d H:i:s') . ' ：' . json_encode($params) . ';' . PHP_EOL, FILE_APPEND);
        $data = $this->service->getHugeAmountAccessToken($params['auth_code']);
    }

    // 刷新巨量开发者平台accessToken(定时任务调用)
    public function refreshToken()
    {
        $this->service->refreshAccessToken();
    }

    // 获取抖音巨量所有的广告主账户下拉列表
    public function accountSelectList()
    {
        $params = Request::param();
        $data = $this->service->getAccountSelectList($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success($data);
    }

    // 快手开放平台回调地址（获取 accessToken）
    public function kwaiCallback()
    {
        $params = Request::param();
        file_put_contents('./back_data.txt', date('Y-m-d H:i:s') . ' ：' . json_encode($params) . ';' . PHP_EOL, FILE_APPEND);
        $data = $this->service->getKwaiAccessToken($params['auth_code']);
    }

    // 快手监测链接
    public function kwaiMonitoringLink()
    {
        $params = Request::param();
        $this->service->saveKwaiMonitoringLinkData($params);
    }

    // 获取快手账户下拉列表
    public function kwaiAccountSelectList()
    {
        $params = Request::param();
        $data = $this->service->getKwaiAccountSelectList($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success($data);
    }

    // 百度授权回调地址
    public function baiduCallback()
    {
        $params = Request::param();
        file_put_contents('./baidu_monitoring_link.txt', date('Y-m-d H:i:s') . ' ：' . json_encode($params) . ';' . PHP_EOL, FILE_APPEND);
        if (empty($params['authCode'])) return;
        $this->service->getBaiduMarketingToken($params);
    }

    // 获取百度信息、子账号
    public function getBaiduUserInfo()
    {
        $data = $this->service->getBaiduUserInfo();
        dump($data);
    }

    // 百度监测链接
    public function baiduMonitoringLink()
    {
        $params = Request::param();
        $this->service->saveBaiduMonitoringLinkData($params);
    }

    // 腾讯监测地址
    public function tencentMonitoringLink()
    {
        $params = Request::param();
        if (empty($params['callback'])) {
            return;
        }
        $this->service->saveTencentMonitoringLinkData($params);
    }

    // 获取小程序URL Scheme
    public function getUrlScheme()
    {
        $params = Request::param();
        $data = $this->service->getWechatUrlScheme($params);
        return json($data);
    }

    // 微博回调地址存储code值
    public function weiboCallback()
    {
        $params = Request::param();
        $this->service->saveWeiboCode($params);
        // file_put_contents('./weibo_monitoring_link.txt', date('Y-m-d H:i:s').' ：'.json_encode($params).';'.PHP_EOL, FILE_APPEND);
    }

    // 微博监测地址
    public function weiboMonitoringLink()
    {
        $params = Request::param();
        $this->service->saveWeiboMonitoringLinkData($params);
    }

    // 妖刀的主体抖音回调地址
    public function tiktokCallback1()
    {
        $params = Request::param();
        file_put_contents('./juliang_callback.txt', date('Y-m-d H:i:s') . ' ：' . json_encode($params) . ';' . PHP_EOL, FILE_APPEND);
        $data = $this->service->getTiktokSubjectAuthAccessToken($params['auth_code'], 8);
    }

    // 抖音主体授权地址
    public function tiktokSubjectAuth()
    {
        $params = Request::param();
        file_put_contents('./juliang_callback.txt', date('Y-m-d H:i:s') . ' ：' . json_encode($params) . ';' . PHP_EOL, FILE_APPEND);
        $data = $this->service->getTiktokSubjectAuthAccessToken($params['auth_code'], 24);
        if ($data) {
            return Common::fail($data);
        }
    }

    // 获取主体
    public function getSubject()
    {
        $params = Request::param();
        if (empty($params['media_id'])) {
            return Common::fail(Error::MEDIA_ID_NOT_EXIXTS);
        }

        $data = $this->service->createLinkSelectObject($params['media_id'], $this->getUserInfo());

        return Common::success($data);
    }

    // 批量导出
    public function batchExportLink()
    {
        $params = Request::param();

        $this->service->batchExportLink($this->getUserInfo(), $params);

        return Common::success();
    }
    // 批量关闭
    public function batchClose()
    {
        $params = Request::param();

        $this->service->batchCloseLink($this->getUserInfo(), $params);

        return Common::success();
    }
    // 分销商用推广链接列表
    public function distributionLinkList()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Order::class)->scene('distribution_link_list')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getDistributionLinkList($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    public function hcInfo(){
        return view();
    }

    // 开关链接
    public function switchLink()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Link::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->switchLink($params['id']);

        if (empty($result)) {
            return Common::fail();
        }

        return Common::success();
    }
}